package com.mujin.keji.collectionsystem.module.main.views;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseFragment;
import com.mujin.keji.collectionsystem.common.CommonPagerAdapter;
import com.mujin.keji.collectionsystem.module.main.adapter.ViewPagerIndicator;
import com.mujin.keji.collectionsystem.module.main.viewmodel.AdvertiseViewModel;
import com.mujin.keji.collectionsystem.module.message.dto.MsgDetail;
import com.mujin.keji.collectionsystem.module.message.viewmodel.MsgViewModel;
import com.mujin.keji.collectionsystem.module.message.views.MessageActivity;
import com.mujin.keji.collectionsystem.module.outboundtask.views.OutBoundtaskListActivity;
import com.mujin.keji.collectionsystem.module.updata.views.UpLoadAllUserDataActivity;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页面左边fragment碎片
 *
 * @author weishixiong
 * @Time 2018-03-19
 */
public class LeftFragment extends BaseFragment implements View.OnClickListener {
    private ViewPager viewPager;
    private LinearLayout llIndcator;
    private CommonPagerAdapter commonPagerAdapter;
    private LinearLayout llVisit;
    private LinearLayout llUpload;
    private LinearLayout llOneCall;
//    private LinearLayout llSign;
    private AdvertiseViewModel advertiseViewModel;
    private List<View> advertiseViews;
    private MsgViewModel msgViewModel;
    private TextView tvMsgContent;
    private TextView tvMsgDate;
    private RelativeLayout rlMsg;
    private List<MsgDetail> msgDetailList;
    private SmartRefreshLayout refreshLayout;



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }


    @Override
    protected void createViewModel() {
        advertiseViewModel = ViewModelProviders.of(this).get(AdvertiseViewModel.class);
        msgViewModel = ViewModelProviders.of(this).get(MsgViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_left;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        llIndcator = (LinearLayout) view.findViewById(R.id.ll_indcator);
        llVisit = (LinearLayout) view.findViewById(R.id.ll_visit);
        llUpload = (LinearLayout) view.findViewById(R.id.ll_upload);
        llOneCall = (LinearLayout) view.findViewById(R.id.ll_one_call);
//        llSign = (LinearLayout) view.findViewById(R.id.ll_sign);
        refreshLayout = (SmartRefreshLayout) view.findViewById(R.id.refreshLayout);
        tvMsgContent = (TextView) view.findViewById(R.id.tv_msg_content);
        tvMsgDate = (TextView) view.findViewById(R.id.tv_msg_date);
        rlMsg = (RelativeLayout) view.findViewById(R.id.rl_msg);
        refreshLayout.setEnableLoadMore(false);
        advertiseViews = new ArrayList<>();

    }


    /**
     * 初始化banner
     */
    private void initBananer() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_advertise, null);
        final ImageView iv_advertise = (ImageView) view.findViewById(R.id.iv_advertise);
        iv_advertise.setImageResource(R.drawable.image_no_bananer);
        advertiseViews.add(view);
        viewPager.addOnPageChangeListener(new ViewPagerIndicator(getActivity(), viewPager, llIndcator, advertiseViews.size()));
        commonPagerAdapter = new CommonPagerAdapter(advertiseViews);
        viewPager.setAdapter(commonPagerAdapter);


    }

    @Override
    public void onPause() {
        super.onPause();
        refreshLayout.finishRefresh();
    }

    @Override
    protected void initData() {
        getMsgList();
        initBananer();
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    /**
     * 获取通知数据
     */
    private void getMsgList() {
        tvMsgContent.setText("暂无消息通知。");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }



    @Override
    protected void initListener() {
        llVisit.setOnClickListener(this);
        llUpload.setOnClickListener(this);
        llOneCall.setOnClickListener(this);
        rlMsg.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getMsgList();
//
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_visit:
                startActivity(new Intent(getActivity(), OutBoundtaskListActivity.class));
                break;
            case R.id.ll_upload:
                startActivity(new Intent(getActivity(), UpLoadAllUserDataActivity.class));
                break;

            case R.id.ll_one_call:
                Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + 110));//跳转到拨号界面，同时传递电话号码
                dialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(dialIntent);
                break;
            case R.id.rl_msg:
                if (EmptyUtils.isNotEmpty(msgDetailList)) {
                    startActivity(new Intent(getActivity(), MessageActivity.class));
                }
                break;
        }
    }
}
